package com.yuandian.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.yuandian.po.BaseSearch;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 分区操作工具类
 */
public class PartitionUtil {

	public static void main(String[] args) {
		BaseSearch baseSearch = new BaseSearch();
		System.out.println(getTimeIntervalList("2023-05-25 19:29:00", 5, 20));

		System.out.println(YDateUtil.yyyy_MM_dd_000000("2023-05-25 19:29:00"));
	}

	/**
	 * 获取时间间隔
	 * @param dataTime	时间
	 * @param interval	间隔
	 * @param num	数量
	 */
	public static List<BaseSearch> getTimeIntervalList(String dataTime, int interval, int num) {
		List<BaseSearch> timeIntervalList = new ArrayList<>();

		for (int i = 0; i < num; i++) {
			String endTime = YDateUtil.yyyy_MM_dd_000000(dataTime);
			String startTime = YDateUtil.getBeforeDayTime(endTime, interval);
			dataTime = startTime;
			BaseSearch baseSearch = new BaseSearch();
			baseSearch.setStartTime(startTime);
			baseSearch.setEndTime(endTime);
			timeIntervalList.add(baseSearch);
		}

		return timeIntervalList;

	}

	/**
	 * 根据endTime获取之前指定数量的分区，并判断是否保留endTime对应的分区
	 * @param endTime	时间
	 * @param partitionNum	分区数量
	 * @param isDelLastPartition	是否保留endTime对应的分区
	 */
	public static List<String> getPartitionListByEndTime(String endTime, int partitionNum, boolean isDelLastPartition) {
		if (partitionNum <= 0) { return null; }

		Set<String> partitionSet = new LinkedHashSet<>();
		String timeVal = (isDelLastPartition ? endTime :  YDateUtil.getBeforeDayTime(endTime, 1));

		for (int i = 0; i < partitionNum; i++) {
			partitionSet.add(getPartitionBasedOnTime(timeVal));
			timeVal = YDateUtil.getBeforeDayTime(timeVal, 1);
		}

		return new ArrayList<>(partitionSet);
	}


    /**
     * 获取 Doris 分区列表(适用情况：doris表按照时间且每天一个分区)
     * @param timeValList 时间列表
	 */
    public static List<String> getPartitionBasedOnTime(List<String> timeValList) {
        Set<String> partitionSet = new LinkedHashSet<>();

        for (String timeVal : timeValList) {
            partitionSet.add(getPartitionBasedOnTime(timeVal));
        }

        return new ArrayList<>(partitionSet);
    }


    /**
     * 获取 Doris 分区(适用情况：doris表按照时间且每天一个分区)
     * @param timeVal 时间
	 */
    public static String getPartitionBasedOnTime(String timeVal) {
        if (StringUtils.isEmpty(timeVal)) { return null; }
        String yyyymmdd = formatDate(DateUtil.parse(timeVal), "yyyyMMdd");
        return "p" + yyyymmdd;
    }


	/**
	 * 获取 Doris 分区(适用情况：doris表按照时间且每小时一个分区)
	 * @param timeVal 时间
	 */
	public static String getPartitionBasedOnHouse(String timeVal) {
		if (StringUtils.isEmpty(timeVal)) { return null; }
		String yyyymmdd = formatDate(DateUtil.parse(timeVal), "yyyyMMddHH");
		return "p" + yyyymmdd;
	}


	/**
	 * 格式化时间
	 */
	private static String formatDate(DateTime timeVal, String formatVal) {
		return DateUtil.format(timeVal, formatVal);
	}


}
